package com.framework.web.bus.repository.specifications;

import com.framework.core.util.LikePatternUtil;
import com.framework.core.util.StringUtil;
import com.framework.web.bus.domain.GoodsInfo;
import com.framework.web.common.domain.BusinessConstants;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 * @author laowang
 * @version V1.0
 * @Title: Specs
 * @Description: 分页查询
 * @date 2016-09-11
 */
public class GoodsInfoSpecs {

    /**
     * 分页查询
     *
     * @param goodsInfo
     * @return
     */
    public static Specification<GoodsInfo> pageQueryGoodsInfo(
            final GoodsInfo goodsInfo) {

        return new Specification<GoodsInfo>() {
            @Override
            public Predicate toPredicate(Root<GoodsInfo> root,
                                         CriteriaQuery<?> query, CriteriaBuilder builder) {
                Predicate predicate = builder.and();
                // equal codeCategory
/*
    if (StringUtil.isNotEmpty( goodsInfo.getGoodsInfoType())) {
        predicate = builder.and(predicate, builder.like(
        builder.lower(root.<String>get(" goodsInfoType")),
        LikePatternUtil.getLikePattern( goodsInfo.getGoodsInfoType())));
    }
  */
                if (StringUtil.isNotEmpty(goodsInfo.getTitle())) {
                    predicate = builder.and(predicate, builder.like(
                            builder.lower(root.<String>get("title")),
                            LikePatternUtil.getLikePattern(goodsInfo.getTitle())));
                }
                predicate = builder.and(predicate,builder.equal(root.<Integer>get("status"), BusinessConstants.STATUS.NORMAL));
                return predicate;
            }
        };
    }


}
